package com.demo.task;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

import java.io.IOException;
import java.net.URL;

/**
 * Created by VietBui on 11/30/13.
 */
public abstract class BitmapDownload extends AsyncTask<String,Void,Bitmap>{

    @Override
    protected final Bitmap doInBackground(String... strings) {

        try {
            URL url = new URL(strings[0]);
            final Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            return  image;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  null;
    }

    @Override
    protected final void onPostExecute(Bitmap bitmap) {
        if(null!=bitmap){
            onDownloadFinish(bitmap);
        }else{
             onError();
        }
    }

    public abstract void onDownloadFinish(Bitmap bitmap);
    public abstract  void onError();
}
